package com.heima.crm.aop;

import com.alibaba.fastjson.JSONObject;
import com.heima.crm.common.utils.BaseContext;
import com.heima.crm.common.utils.JwtUtils;
import com.heima.crm.mapper.CrUserMapper;
import com.heima.crm.mapper.SysOperLogMapper;
import com.heima.crm.pojos.entity.SysOperLog;
import com.heima.crm.pojos.entity.TbUser;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@Aspect
@Slf4j
public class LogAspect {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysOperLogMapper sysOperLogMapper;
    @Autowired
    private CrUserMapper crUserMapper;

    @Pointcut("execution(* com.heima.crm.controller.*.*(..)))")
    public void logPointCut(){
    }

    @Pointcut("execution(* com.heima.crm.controller.CrUserLoginController.*(..))")
    public void excludePointCun(){

    }

    @Around("logPointCut() && !excludePointCun()")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable{
        SysOperLog sysOperLog = new SysOperLog();

        //操作时间
        sysOperLog.setOperTime(LocalDateTime.now());

        //请求ip
        String ipAddr = request.getRemoteAddr();
        sysOperLog.setOperIp(ipAddr);

        //获取操作人
        //String token = request.getHeader("token");
        //Claims claims = JwtUtils.parseJWT(token);
        Long id = BaseContext.getCurrentId();
        TbUser tbUser = crUserMapper.selectById(id);
        sysOperLog.setOperName(tbUser.getUserName());

        //获取请求路径
        String requestURI = request.getRequestURI();
        sysOperLog.setOperUrl(requestURI);

        //获取请求方式
        String method = request.getMethod();
        sysOperLog.setRequestMethod(method);

        //获取请求模块
        if(requestURI.contains("clue")){
            sysOperLog.setTitle("线索管理");
        }else if (requestURI.contains("contract")){
            sysOperLog.setTitle("合同管理");
        }else if (requestURI.contains("role")){
            sysOperLog.setTitle("角色管理");
        }else if (requestURI.contains("user")){
            sysOperLog.setTitle("用户管理");
        }else if (requestURI.contains("dept")){
            sysOperLog.setTitle("部门管理");
        }else if (requestURI.contains("index")){
            sysOperLog.setTitle("首页");
        }else if (requestURI.contains("post")){
            sysOperLog.setTitle("岗位管理");
        }else if (requestURI.contains("statistics")){
            sysOperLog.setTitle("统计模块");
        }else if (requestURI.contains("dicData")){
            sysOperLog.setTitle("字典模块");
        }else if (requestURI.contains("dic")){
            sysOperLog.setTitle("字典管理");
        }else if (requestURI.contains("menu")){
            sysOperLog.setTitle("菜单管理");
        }else if (requestURI.contains("notice")){
            sysOperLog.setTitle("通知管理");
        }else if (requestURI.contains("activity")){
            sysOperLog.setTitle("活动管理");
        }else if (requestURI.contains("business")){
            sysOperLog.setTitle("商机管理");
        }else if (requestURI.contains("course")){
            sysOperLog.setTitle("课程管理");
        }else if (requestURI.contains("transfer")){
            sysOperLog.setTitle("转派管理");
        }else if (requestURI.contains("systemLogs")){
            sysOperLog.setTitle("系统日志");
        }

        //获取方法名
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        String methodName = signature.getMethod().getName();
        sysOperLog.setMethod(methodName);

        //获取请求参数
        Object[] args = joinPoint.getArgs();
        String argsString = Arrays.toString(args);
        sysOperLog.setOperParam(argsString);


        Object result = joinPoint.proceed();

        String returnValue = JSONObject.toJSONString(result);

        sysOperLogMapper.insert(sysOperLog);

        log.info("AOP记录操作日志：{}",sysOperLog);

        return result;
    }
}
